package com.tgy.MyFlowable.workflow.service.impl;

import com.ctsi.core.common.util.StringUtil;
import com.ctsi.workflow.dto.FlwHandleTaskDTO;
import com.ctsi.workflow.service.IFlwCommonTaskService;
import com.ctsi.workflow.service.IFlwHandleTaskService;
import com.ctsi.workflow.service.IFlwStartProcessService;
import com.ctsi.workflow.service.IFlwSubmitTaskService;
import com.google.gson.Gson;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @author: wang xiao xiang
 * @date: 2021/8/20 8:34
 */
@Service
@RequiredArgsConstructor
public class FlwHandleTaskServiceImpl implements IFlwHandleTaskService {

    private final IFlwStartProcessService flwStartProcessService;
    private final IFlwSubmitTaskService flwSubmitTaskService;
    private final IFlwCommonTaskService flwCommonTaskService;

    @Override
    public void start(FlwHandleTaskDTO flwHandleTaskDTO) {
        String processDefinitionId = flwHandleTaskDTO.getProcessDefinitionId();
        Map<String, String> variables = flwHandleTaskDTO.getVariables();
        String nextAssignee = flwHandleTaskDTO.getNextAssignee();
        String nextDueDate = flwHandleTaskDTO.getNextDueDate();
        Integer nextPriority = flwHandleTaskDTO.getNextPriority();
        flwStartProcessService.start(processDefinitionId, variables, nextAssignee, nextDueDate, nextPriority);
    }

    @Override
    public void submit(FlwHandleTaskDTO flwHandleTaskDTO) {
        String taskId = flwHandleTaskDTO.getTaskId();
        String comment = flwHandleTaskDTO.getComment();
        Map<String, String> variables = flwHandleTaskDTO.getVariables();
        String formData = variables.get("formData");
        Gson gson = new Gson();
        Map<String, Object> map = new HashMap<String, Object>();
        map = gson.fromJson(formData, map.getClass());
        String nextAssignee = flwHandleTaskDTO.getNextAssignee();
        if (StringUtil.isNotBlank(nextAssignee)) {
            List<String> nextAssigneeList = Arrays.asList(nextAssignee.split(","));
            map.put("assigneeList", nextAssigneeList);
        }
        String nextDueDate = flwHandleTaskDTO.getNextDueDate();
        Integer nextPriority = flwHandleTaskDTO.getNextPriority();
        flwSubmitTaskService.submit(taskId, comment, map, nextAssignee, nextDueDate, nextPriority);
    }

    @Override
    public void save(FlwHandleTaskDTO flwHandleTaskDTO) {
        String taskId = flwHandleTaskDTO.getTaskId();
        Map<String, String> variables = flwHandleTaskDTO.getVariables();
        flwSubmitTaskService.save(taskId, variables);
    }

    @Override
    public void back(FlwHandleTaskDTO flwHandleTaskDTO) {
        String taskId = flwHandleTaskDTO.getTaskId();
        String targetActId = flwHandleTaskDTO.getTargetActId();
        String targetActName = flwHandleTaskDTO.getTargetActName();
        String comment = flwHandleTaskDTO.getComment();
        flwCommonTaskService.back(taskId, targetActId, targetActName, comment);
    }

    @Override
    public void turn(FlwHandleTaskDTO flwHandleTaskDTO) {
        String taskId = flwHandleTaskDTO.getTaskId();
        List<String> assigneeList = flwHandleTaskDTO.getAssigneeList();
        flwCommonTaskService.turnTask(taskId, assigneeList);
    }

    @Override
    public void entrust(FlwHandleTaskDTO flwHandleTaskDTO) {
        String taskId = flwHandleTaskDTO.getTaskId();
        String assignee = flwHandleTaskDTO.getAssignee();
        String comment = flwHandleTaskDTO.getComment();
        flwCommonTaskService.entrust(taskId, assignee, comment);
    }

    @Override
    public void jump(FlwHandleTaskDTO flwHandleTaskDTO) {
        String taskId = flwHandleTaskDTO.getTaskId();
        String targetActId = flwHandleTaskDTO.getTargetActId();
        String targetActName = flwHandleTaskDTO.getTargetActName();
        String comment = flwHandleTaskDTO.getComment();
        flwCommonTaskService.jump(taskId, targetActId, targetActName, comment);
    }

    @Override
    public void end(FlwHandleTaskDTO flwHandleTaskDTO) {
        String taskId = flwHandleTaskDTO.getTaskId();
        String comment = flwHandleTaskDTO.getComment();
        flwCommonTaskService.end(taskId, comment);
    }

    @Override
    public void suspend(FlwHandleTaskDTO flwHandleTaskDTO) {
        String taskId = flwHandleTaskDTO.getTaskId();
        flwCommonTaskService.suspend(taskId);
    }

    @Override
    public void addSign(FlwHandleTaskDTO flwHandleTaskDTO) {
        String taskId = flwHandleTaskDTO.getTaskId();
        List<String> assigneeList = flwHandleTaskDTO.getAssigneeList();
        String comment = flwHandleTaskDTO.getComment();
        flwCommonTaskService.addSign(taskId, assigneeList, comment);
    }

    @Override
    public void delSign(FlwHandleTaskDTO flwHandleTaskDTO) {
        String taskId = flwHandleTaskDTO.getTaskId();
        List<String> assigneeList = flwHandleTaskDTO.getAssigneeList();
        String comment = flwHandleTaskDTO.getComment();
        flwCommonTaskService.delSign(taskId, assigneeList, comment);
    }

    @Override
    public Map<String, Object> taskData(FlwHandleTaskDTO flwHandleTaskDTO) {
        String taskId = flwHandleTaskDTO.getTaskId();
        return flwCommonTaskService.taskData(taskId);
    }
}
